package com.upbos.boot.sso.configuration;

import com.upbos.boot.sso.props.*;
import com.upbos.sso.plugin.*;
import com.upbos.boot.sso.annotation.ConditionalOnExistProperty;
import com.upbos.boot.sso.props.*;
import com.upbos.sso.Constants;
import com.upbos.sso.filter.SsoFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;


@Configuration
@EnableConfigurationProperties(value = {InterceptorProps.class})
@ConditionalOnClass(SsoFilter.class)
public class InterceptorAutoConfiguration {

    @Resource
    private InterceptorProps interceptorProps;

    @Bean
    @ConditionalOnExistProperty(prefix = "sso.interceptors", name = Constants.INTERCEPTOR_TOKEN)
    public TokenInterceptor tokenInterceptor() {
        TokenInterceptor tokenInterceptor = new TokenInterceptor();
        InterceptorTokenProps tokenProps = interceptorProps.getToken();
        if (tokenProps == null) {
            return tokenInterceptor;
        }

        if (tokenProps.getExpireUrl() != null) {
            tokenInterceptor.setExpireUrl(tokenProps.getExpireUrl());
        }

        if (tokenProps.getExpireUrls() != null) {
            tokenInterceptor.setExpireUrls(tokenProps.getExpireUrls());
        }
        return tokenInterceptor;
    }

    @Bean
    @ConditionalOnExistProperty(prefix = "sso.interceptors", name = Constants.INTERCEPTOR_RES)
    public ResInterceptor resInterceptor() {
        ResInterceptor resInterceptor = new ResInterceptor();
        InterceptorResProps resProps = interceptorProps.getRes();
        if (resProps == null) {
            return resInterceptor;
        }

        if (resProps.getExcludeUrls() != null) {
            resInterceptor.setExcludeUrls(resProps.getExcludeUrls());
        }

        return resInterceptor;
    }

    @Bean
    @ConditionalOnExistProperty(prefix = "sso.interceptors", name = Constants.INTERCEPTOR_SSO_SERVER_LOGIN)
    public SsoServerLoginInterceptor ssoServerLoginInterceptor() {

        SsoServerLoginInterceptor ssoServerLoginInterceptor = new SsoServerLoginInterceptor();
        InterceptorSsoServerLoginProps ssoServerLoginProps = interceptorProps.getSsoServerLogin();
        if (ssoServerLoginProps == null) {
            return ssoServerLoginInterceptor;
        }

        if (ssoServerLoginProps.getSsoServerUrl() != null) {
            ssoServerLoginInterceptor.setSsoServerUrl(ssoServerLoginProps.getSsoServerUrl());
        }

        if (ssoServerLoginProps.getSsoServerUrls() != null) {
            ssoServerLoginInterceptor.setSsoServerUrls(ssoServerLoginProps.getSsoServerUrls());
        }

        return ssoServerLoginInterceptor;
    }

    @Bean
    @ConditionalOnExistProperty(prefix = "sso.interceptors", name = Constants.INTERCEPTOR_SSO_SERVER_TOKEN)
    public SsoServerTokenInterceptor ssoServerTokenInterceptor() {
        SsoServerTokenInterceptor ssoServerTokenInterceptor = new SsoServerTokenInterceptor();
        InterceptorSsoServerTokenProps ssoServerTokenProps = interceptorProps.getSsoServerToken();
        if (ssoServerTokenProps == null) {
            return ssoServerTokenInterceptor;
        }

        if (ssoServerTokenProps.getTokenServerUrl() != null) {
            ssoServerTokenInterceptor.setTokenServerUrl(ssoServerTokenProps.getTokenServerUrl());
        }
        return ssoServerTokenInterceptor;
    }

    @Bean
    @ConditionalOnExistProperty(prefix = "sso.interceptors", name = Constants.INTERCEPTOR_SSO_CLIENT)
    public SsoClientInterceptor ssoClientInterceptor() {
        SsoClientInterceptor ssoClientInterceptor = new SsoClientInterceptor();
        InterceptorSsoClientProps ssoClientProps = interceptorProps.getSsoClient();
        if (ssoClientProps == null) {
            return ssoClientInterceptor;
        }

        if (ssoClientProps.getSsoServerUrl() != null) {
            ssoClientInterceptor.setTokenServerUrl(ssoClientProps.getSsoServerUrl());
        }

        return ssoClientInterceptor;
    }

}